package com.wman.sheep.common.utils;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wman.sheep.R;
import com.wman.sheep.common.application.AbsAppContext;

/**
 * Created by Administrator on 2017/2/14.
 */

public class ToastUtil {
    private static Toast toast = null;

    public static void makeText(Context context, String content) {
        if (toast == null) {
            toast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
        } else {
            toast.setText(content);
        }
        toast.show();
    }

    //连续吐司
    public static void showTextToast(String msg) {
        try {
            Activity activity = AbsAppContext.getActivityManager().currentActivity();
            showToast(activity, msg, 0);
        } catch (Exception e) {
            e.printStackTrace();
            showToast(AbsAppContext.context(), msg, 0);
        }
    }

    //连续吐司
    public static void showTextToast(Context context, String msg) {
        showToast(context, msg, 0);
    }

    public static void showToast(Context context, String msg, int icon) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_toast, null);
        ((TextView) view.findViewById(R.id.title_tv)).setText(msg);
        if (icon != 0) {
            ((ImageView) view.findViewById(R.id.icon_iv)).setImageResource(icon);
            ((ImageView) view.findViewById(R.id.icon_iv)).setVisibility(View.VISIBLE);
        }

        if (toast == null) {
            toast = new Toast(context);
        }

        toast.setView(view);
        toast.setDuration(Toast.LENGTH_SHORT);
//        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public static void cancel() {
        if (toast != null) {
            toast.cancel();
            toast = null;
        }
    }
}
